package com.ruoyi.web.controller.warning;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.warning.domain.entity.Syslog;
import com.ruoyi.warning.domain.query.SyslogQuery;
import com.ruoyi.warning.service.ISyslogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author chenhc
 * @since 2025-05-19
 */
@RestController
@RequestMapping("/syslog")
@Api(tags = "系统日志", description = "用于处理系统日志相关操作的控制器")
public class SyslogController {
    @Resource
    private ISyslogService syslogService;
    @ApiOperation("获取系统日志")
    @GetMapping(value = "/getPage")
    public R getPage(SyslogQuery query)
    {
        return R.ok(syslogService.getPage(query));
    }

    @ApiOperation("获取首页的系统日志")
    @GetMapping(value = "/getIndexPage")
    public R getIndexPage(SyslogQuery query)
    {
        LambdaQueryWrapper<Syslog> wrapper = new QueryWrapper<Syslog>().lambda()
                .orderByDesc(Syslog::getTimestamp)
                .last("limit 20");
        List<Syslog> list = syslogService.list(wrapper);
        return R.ok(list);
    }

    @Anonymous
    @ApiOperation("清理过期数据")
    @GetMapping(value = "/clear")
    public R clear()
    {
        syslogService.deleteSyslog();
        return R.ok();
    }
}
